package cn.oms.auth.service.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 设置Security全部放行,因为我们Oauth2的方式验证
    // 原有的session验证就不需要了

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .anyRequest().permitAll()
                .and().formLogin();
    }
    //当前是配置类,我们框架中需要进行加密,我们注入一个对象到Spring容器
    // 确定它的加密规则,这里仍然使用bcrypt
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    // 当前类继承了一个父类,这个父类中有一个对象包含着验证登录的代码
    // 当前Spring容器需要这个对象,把他保存到Spring容器中
    @Bean
    public AuthenticationManager authenticationManagerBean()
                                                throws Exception {
        return  super.authenticationManagerBean();
    }


}
